package cn.show;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.ArrayListHandler;

import cn.com.hkr.jdbc.TxQueryRunner;

import com.alibaba.fastjson.JSON;

@WebServlet("/ShowServlet")
public class ShowServlet extends HttpServlet{
	
	private TxQueryRunner  qr = new TxQueryRunner();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sql = "select dname, count(*) as num from t_employees e,t_dept  d where e.deptno = d.deptno group by dname";
		try {
			List<Object[]> list = qr.query(sql, new ArrayListHandler());
			
			writeJson(list, response);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	private void writeJson(Object obj,HttpServletResponse response){
		try {
			response.getWriter().print(JSON.toJSONString(obj));
			System.out.println(JSON.toJSONString(obj));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
